package com.jax.pronet.unitet.tests;

import com.jax.pronet.bo.*;
import com.jax.pronet.xml.NetworkLoader;
import junit.framework.Assert;
import junit.framework.TestCase;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class PronetJaxBasicUnitTest extends TestCase
{
  private String pronetLogo;

  protected void setUp()
  {
    pronetLogo = "Pronet JAX London 2012 Team";
  }

  protected void tearDown()
  {
    pronetLogo = null;
  }

  public void testSomeOtherBehaviour() throws IOException
  {
    Network n = new NetworkLoader().loadNetwork();
    assertNotNull(n);
  }

  public void testSomeBehavior() throws Exception
  {
    assertNotNull(pronetLogo);
    System.out.println("I've run a test!");
    /*
    Network n = new Network();
    Programmer p = new Programmer();
    Recommendation r = new Recommendation();
    r.setProgrammerName("team A programmer");
    Skill s = new Skill();
    s.setSkillName("Skillname");
    p.setListOfSkills(Arrays.asList("A", "B"));
    p.setListOfRecommendations(Arrays.asList(r));
    n.setNetworkName("Netname");
    n.setListOfProgrammers(Arrays.asList(p));
    */
    JAXBContext c = JAXBContext.newInstance(Network.class, Programmer.class, Recommendation.class, Skill.class);
    Marshaller m = c.createMarshaller();
    Unmarshaller u = c.createUnmarshaller();
    Network n = (Network) u.unmarshal(new File(new File("data"), "ProNet.xml"));
    System.out.println(n);
  }

  public void testKudosOfAB() throws JAXBException
  {
    Network n = new Network();
    List<Programmer> programmers = new ArrayList<Programmer>();
    programmers.add(new Programmer(1, "A", null, Arrays.asList("B")));
    programmers.add(new Programmer(1, "B", null, Arrays.asList("A")));
    n.setListOfProgrammers(programmers);
    Graph graph = new Graph(n);
    Map<String, Double> kudos = graph.calculateKudos();
    Assert.assertEquals("Kudos for A is wrong", 1., kudos.get("A"), 1E-4);
    Assert.assertEquals("Kudos for B is wrong", 1., kudos.get("B"), 1E-4);
  }

  public void testKudosOfSecondTest() throws JAXBException
  {
    Network n = new Network();
    List<Programmer> programmers = new ArrayList<Programmer>();
    programmers.add(new Programmer(1, "A", null, Arrays.asList("B", "C")));
    programmers.add(new Programmer(1, "B", null, Arrays.asList("C")));
    programmers.add(new Programmer(1, "C", null, Arrays.asList("A")));
    programmers.add(new Programmer(1, "D", null, Arrays.asList("C")));
    n.setListOfProgrammers(programmers);
    Graph graph = new Graph(n);
    Map<String, Double> kudos = graph.calculateKudos();
    Assert.assertEquals("Kudos for A is wrong", 1.49, kudos.get("A"), 1E-2);
    Assert.assertEquals("Kudos for B is wrong", 0.78, kudos.get("B"), 1E-2);
    Assert.assertEquals("Kudos for C is wrong", 1.57, kudos.get("C"), 1E-2);
    Assert.assertEquals("Kudos for D is wrong", 0.15, kudos.get("D"), 1E-2);
  }
}
